package com.hjg.baseglide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;

import java.lang.ref.WeakReference;

public class BitmapRequest {
    private Context context;
    private String url;//加载的地址
    private WeakReference<ImageView> imageViewWeakReference;//加载的控件
    private String md5;//需要将imageview的tag设置对应url的md5，设置的时候必须md5相同的时候才会给imageview设置
    private Drawable drawable;//占位图

    public BitmapRequest setPlaceHoder(Drawable drawable) {
        this.drawable = drawable;
        return this;
    }

    public void into(ImageView imageView) {
        imageView.setTag(getMd5());
        setImageView(imageView);
        //这里设置就已经结束了
        //需要将这次的请求加入到队列中
        RequestManager.getInstance().addBitmapRequest(this);
    }

    public String getUrl() {
        return url;
    }

    public BitmapRequest setUrl(String url) {
        this.url = url;
        //设置md5的值
        setMd5(Md5Utils.string2MD5(url));
        Log.d("BitmapRequest", getMd5());
        return this;
    }


    public ImageView getImageView() {
        return imageViewWeakReference.get();
    }

    public BitmapRequest setImageView(ImageView imageView) {
        this.imageViewWeakReference = new WeakReference<>(imageView);
        return this;
    }

    public String getMd5() {
        return md5;
    }

    private void setMd5(String md5) {
        this.md5 = md5;
    }


    public Drawable getDrawable() {
        return drawable;
    }


    public Context getContext() {
        return context;
    }

    public BitmapRequest setContext(Context context) {
        this.context = context;
        return this;
    }
}
